package cn.lf.android.lawfirm.inputsource;


import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.inputsource.conntroller.SourcePeopleController;
import cn.lf.android.lawfirm.inputsource.model.People;
import cn.lf.android.lawfirm.inputsource.view.SourcePeopleAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.view.NoScrollListView;

import static android.R.id.list;

/**
 * 案源人列表
 */
public class TheSourcePeopleListActivity extends OrdinaryBaseActivity
        implements SourcePeopleController.SourcePeopleCallback, AdapterView.OnItemClickListener {

    private SourcePeopleController mSourcePeopleController;
    private ListView mListView, listview_search;
    private SourcePeopleAdapter mAdapter, mAdapter_search;
    private List<People> mList = new ArrayList<People>();
    private List<People> mList_search = new ArrayList<People>();
    private String title = "";
    private String isBusiness = "";
    @Override
    public int getChildView() {
        return R.layout.activity_source_people_list;
    }

    @Override
    public void initViewId() {
        title = getIntent().getStringExtra("title");
        isBusiness= getIntent().getStringExtra("isBusiness");
        initTitle();
        initSearch();
        addTextChangeWatcher();
        mListView = ((ListView) findId(R.id.noScrollLV));
        listview_search = ((ListView) findId(R.id.listview_search));
        mSelectBtn.setOnClickListener(this);
        mSearch.setHint("请输入案源人姓名");
    }

    @Override
    public void initData() {
        //搜索框的×，是用来清除搜索框数据的
        isClearText(true);
        mClean.setOnClickListener(this);
        mSourcePeopleController = new SourcePeopleController(this, this);
        mAdapter = new SourcePeopleAdapter(TheSourcePeopleListActivity.this, mList);
        mAdapter_search = new SourcePeopleAdapter(TheSourcePeopleListActivity.this, mList_search);
        listview_search.setAdapter(mAdapter_search);
        listview_search.setOnItemClickListener(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        mSourcePeopleController.onSourcePeople(isBusiness);
    }

    private void addTextChangeWatcher() {

//        mSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                pageIndex = 1;
//                list.clear();
//                mUnderListConroller.onUnderList(mSearch.getText().toString().trim(), String.valueOf(pageIndex), state);
//                return false;
//            }
//        });
        mSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //这里是联通的，有变化就进行网络请求
                mList_search.clear();
                if (mSearch.getText().toString().trim().length() == 0) {
                    mClean.setVisibility(View.GONE);
                    listview_search.setVisibility(View.GONE);
                    return;
                } else {
                    for (People p : mList) {
                        if (p.userName.contains(charSequence.toString())) {
                            mList_search.add(p);
                        }
                    }
                    mAdapter_search.notifyDataSetChanged();
                    if (mList_search.size() > 0) {
                        listview_search.setVisibility(View.VISIBLE);
                    }
                    mClean.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private void initSearch() {
        mSearchLayout.setVisibility(View.VISIBLE);
        mSelectBtn.setVisibility(View.GONE);
        mClean.setVisibility(View.GONE);
        mSelectBtn.setOnClickListener(this);
    }

    private void initTitle() {
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setOnClickListener(this);
        mTitle.setText(title);
    }

    /**
     * 点击屏幕重新加载
     */
    @Override
    public void refreshLoad() {

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.img_left:
                hintKbTwo();
                finish();
                break;
            case R.id.search:
                hintKbTwo();
//                DebugLog.e("------输入框中内容带回上一页-------" + mSearch.getText());
//                Intent intent = new Intent();
//                intent.putExtra("name", mSearch.getText().toString());
//                setResult(123, intent);
//                finish();
                break;
            case R.id.imgClean:
                hintKbTwo();
                mSearch.setText("");
                break;
            default:
                break;
        }
    }


    @Override
    public void onSourcePeopleSuccessed(List<People> bean) {
        mList.clear();
        mList.addAll(bean);
        mAdapter.notifyDataSetChanged();
    }


    @Override
    public void onSourcePeopleFailed(String errorMsg) {
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //点击条目，将数据带回前一页
        DebugLog.e("----i---" + i);
        DebugLog.e("------mList.get(i)------" + mList.get(i));
        hintKbTwo();
        Intent intent = new Intent();
        if (listview_search.getVisibility() == View.VISIBLE) {
            intent.putExtra("people", mList_search.get(i));
        } else {
            intent.putExtra("people", mList.get(i));
        }
        setResult(RESULT_OK, intent);
        finish();
//                    * cusID字段：客户编号（int）
//                    * name字段：客户名称（string）
//                    * type字段：客户类型（int）
//                    * linkmen字段：联系人（string）
//                    * linkTel字段：联系电话（string）
//        intent.putExtra("cusID", mList.get(i).getCusID());
//        intent.putExtra("linkmen", mList.get(i).getLinkmen());
//        intent.putExtra("type", mList.get(i).getType());
//        intent.putExtra("linkTel", mList.get(i).getLinkTel());
//        DebugLog.e("------.get(i).getCusID()------" + mList.get(i).getCusID());

    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

}